package com.objecteffects.template.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import org.apache.commons.lang3.builder.ToStringBuilder;

@Entity
@NamedQueries({
@NamedQuery(name = "UserEntity.findByUserName",
    query = "SELECT u FROM UserEntity as u WHERE u.userName = :userName"),
        @NamedQuery(name = "UserEntity.findByHash",
        query = "SELECT u FROM UserEntity as u WHERE u.hash = :hash"),
})
public class UserEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false, unique = true)
    private String userName;

    @Column(nullable = true)
    private String tumblrName;

    @Column(nullable = false)
    private String password;

    @OneToMany(orphanRemoval = true, cascade = {
        CascadeType.ALL
    }, fetch = FetchType.EAGER)
    private List<TumblrBlogEntity> tumblrBlogEntities = new ArrayList<>();

    @Column(unique = true)
    private String hash;

    public UserEntity() {
        //
    }

    public UserEntity(final String _username, final String _password) {
        this.userName = _username;
        this.password = _password;
    }

    public UserEntity(final String _username, final String _password,
        final String _hash) {
        this.userName = _username;
        this.password = _password;
        this.hash = _hash;
    }

    /**
     * @return the Id
     */
    public final Long getId() {
        return this.id;
    }

    /**
     * @param _id
     *        the id to set
     */
    public final void setId(final Long _id) {
        this.id = _id;
    }

    /**
     * @return the userName
     */
    public final String getUserName() {
        return this.userName;
    }

    /**
     * @param _username
     *        the userName to set
     */
    public final void setUserName(final String _username) {
        this.userName = _username;
    }

    /**
     * @return the tumblrName
     */
    public final String getTumblrName() {
        return this.tumblrName;
    }

    /**
     * @param _tumblrName
     *        the tumblrName to set
     */
    public final void setTumblrName(final String _tumblrName) {
        this.tumblrName = _tumblrName;
    }

    /**
     * @return the password
     */
    public final String getPassword() {
        return this.password;
    }

    /**
     * @param _password
     *        the password to set
     */
    public final void setPassword(final String _password) {
        this.password = _password;
    }

    // /**
    // * @return the tumblrBlogEntities
    // */
    // public Map<String, Token> getTumblrBlogs() {
    // return this.tumblrBlogs;
    // }

    // /**
    // * @param tumblrBlogEntities
    // * the tumblrBlogEntities to set
    // */
    // public void setTumblrBlogs(final Map<String, Token>
    // tumblrBlogEntities) {
    // this.tumblrBlogs = tumblrBlogEntities;
    // }

    /**
     * @return the tumblrBlogEntities
     */
    public final List<TumblrBlogEntity> getTumblrBlogEntities() {
        return this.tumblrBlogEntities;
    }

    /**
     * @param _tumblrBlogEntities
     *        the tumblrBlogEntities to set
     */
    public final void setTumblrBlogEntities(
        final List<TumblrBlogEntity> _tumblrBlogEntities) {
        this.tumblrBlogEntities = _tumblrBlogEntities;
    }

    /**
     * @return the hash
     */
    public final String getHash() {
        return this.hash;
    }

    /**
     * @param _hash
     *        the hash to set
     */
    public final void setHash(final String _hash) {
        this.hash = _hash;
    }

    @Override
    public final String toString() {
        return new ToStringBuilder(this).append("id", this.id)
            .append("userName", this.userName)
            .append("password", this.password)
            .append("tumblrBlogEntities", this.tumblrBlogEntities).toString();
    }

    @Override
    public final int hashCode() {
        return Objects.hash(this.id, this.userName);
    }

    @Override
    public final boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null) {
            return false;
        }

        if (this.getClass() != obj.getClass()) {
            return false;
        }

        final UserEntity other = (UserEntity) obj;

        if (this.id == null) {
            if (other.id != null) {
                return false;
            }
        }
        else if (!this.id.equals(other.id)) {
            return false;
        }

        if (this.userName == null) {
            if (other.userName != null) {
                return false;
            }
        }
        else if (!this.userName.equals(other.userName)) {
            return false;
        }

        return true;
    }
}
